home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / WINDOWS / AUDIOCD.ARJ / CD.C < prev    next >
C/C++ Source or Header  |  1992-10-24  |  9KB  |  267 lines

  1.  /***********************************************************************/
  2.  /* Simple CD Player --- 24 October 1992                                */
  3.  /*                                                                     */
  4.  /* Written by John A. Junod using code ideas presented in the SDK      */
  5.  /* documentation and uses the basic code form from Quick Case.         */
  6.  /*                                                                     */
  7.  /* This program opens "CDAUDIO" when the program starts, keeps it open */
  8.  /* while the program is running and closes it on exit.  This does make */
  9.  /* some actions take less time.  Error recovery is basically non-      */
  10.  /* existant beyond giving the user a basically useless error message.  */
  11.  /*                                                                     */
  12.  /* This program has no "main" window and relies on a dialog box to do  */
  13.  /* everything.                                                         */
  14.  /*                                                                     */
  15.  /* Modify this as much as you wish and redistribute however you wish,  */
  16.  /* but I do ask that you give me some of the credit and that you let   */
  17.  /* other people use the final product for FREE and don't charge some   */
  18.  /* silly shareware fee of $25.                                         */
  19.  /***********************************************************************/
  20.  
  21. #include "CD.h"
  22.  
  23. HWND hInst;
  24. HWND hMainWnd;
  25.  
  26. char szBuffer[4096];  /* general purpose large buffer */
  27. char szBuf[256];      /* general purpose small buffer */
  28. char szTitle[100];    /* window title */
  29.  
  30. UINT wDeviceID;       /* open cd device id */
  31. int iCDstatus;        /* cd status */
  32.  
  33. #define CD_STOPPED 0
  34. #define CD_PLAYING 1
  35. #define CD_PAUSED  2
  36. #define CD_NOTRDY  3
  37. #define CD_OPEN    4
  38. char *szStatus[5]={"stopped","playing","paused","notready","open"};
  39.  
  40. HCURSOR hHourGlass;
  41. HCURSOR hSaveCursor;
  42. HICON hIcon;
  43. BOOL bCanEject;
  44.  
  45. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  46.                    LPSTR lpszCmdLine, int nCmdShow)
  47. {
  48.  /***********************************************************************/
  49.  /* HANDLE hInstance;       handle for this instance                    */
  50.  /* HANDLE hPrevInstance;   handle for possible previous instances      */
  51.  /* LPSTR  lpszCmdLine;     long pointer to exec command line           */
  52.  /* int    nCmdShow;        Show code for main window display           */
  53.  /***********************************************************************/
  54.  
  55.     MCI_OPEN_PARMS mciOpenParms;
  56.     FARPROC    lpMainDlg;
  57.     DWORD        dwMCIerr;
  58.  
  59.     hInst = hInstance;
  60.     if(hPrevInstance) return(0);
  61.     hHourGlass=LoadCursor(NULL,IDC_WAIT);
  62.     hIcon=LoadIcon(hInst,"CDICON");
  63.  
  64.     // open the compact disc device
  65.     mciOpenParms.lpstrDeviceType="cdaudio";
  66.     if(dwMCIerr=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_TYPE,
  67.             (DWORD)(LPVOID)&mciOpenParms)) {
  68.         // failed to open, show the error and end this program.
  69.         showMCIError(dwMCIerr);
  70.         return(FALSE);
  71.     }
  72.     // opened successfully
  73.     wDeviceID=mciOpenParms.wDeviceID;
  74.  
  75.     if(dwMCIerr=CD_GetDeviceInfo(wDeviceID))
  76.         showMCIError(dwMCIerr);
  77.  
  78.     iCDstatus=CD_GetCurrentStatus(wDeviceID);
  79.  
  80.     if((lpMainDlg=MakeProcInstance((FARPROC)MainDlg,hInst))==NULL)
  81.          MessageBox(NULL,"MakeProc Failed","CD Error",MB_OK);
  82.     else
  83.         if(DialogBox(hInst,"CDBox",hMainWnd,lpMainDlg)== -1)
  84.             MessageBox(NULL,"Couldn't create dialog box","CD Error",MB_OK);
  85.         else
  86.             FreeProcInstance(lpMainDlg);
  87.  
  88.     // close the CD device and exit program
  89.     if(dwMCIerr=mciSendCommand(wDeviceID,MCI_CLOSE,0,NULL))
  90.         showMCIError(dwMCIerr);
  91.     return TRUE;
  92. } /*  End of WinMain                                                    */
  93.  
  94.  
  95. BOOL FAR PASCAL MainDlg(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  96. {
  97.     DWORD dwMCIerr;
  98.     UINT uAUXerr;
  99.     DWORD volume;
  100.     HMENU hMenu;
  101.  
  102.     switch(message) {
  103.         case WM_INITDIALOG:
  104.             // change the system menus of this dialog box!
  105.             hMenu=GetSystemMenu(hDlg,FALSE);
  106.             RemoveMenu(hMenu,4,MF_BYPOSITION);
  107.             AppendMenu(hMenu,MF_SEPARATOR,0,NULL);
  108.             AppendMenu(hMenu,MF_STRING,IDM_SHAUTH,"About...");
  109.             AppendMenu(hMenu,MF_SEPARATOR,0,NULL);
  110.             AppendMenu(hMenu,MF_STRING,IDM_EJECT,"Eject");
  111.             AppendMenu(hMenu,MF_STRING,IDM_STOP,"Stop");
  112.             AppendMenu(hMenu,MF_STRING,IDM_PAUSE,"Pause");
  113.             AppendMenu(hMenu,MF_STRING,IDM_PLAY,"Play");
  114.             AppendMenu(hMenu,MF_STRING,IDM_BACK,"Backward");
  115.             AppendMenu(hMenu,MF_STRING,IDM_FORWARD,"Forward");
  116.             AppendMenu(hMenu,MF_STRING,IDM_STATUS,"Tracks...");
  117.             // sub-routines use the global hMainWnd as the window to notify
  118.             hMainWnd=hDlg;
  119.             // format the window title line
  120.             wsprintf(szTitle,"CD (%s) %d",
  121.                 (LPSTR)szStatus[CD_GetCurrentStatus(wDeviceID)],
  122.                 CD_GetCurrentTrack(wDeviceID));
  123.             // display new title
  124.             SetWindowText(hDlg,szTitle);
  125.             // set a timer ~5 seconds to update title
  126.             SetTimer(hDlg,1,5000,NULL);
  127.             return(TRUE);
  128.  
  129.         // timer click updates title to reflect status of CD
  130.         case WM_TIMER:
  131.         case MM_MCINOTIFY:
  132.             iCDstatus=CD_GetCurrentStatus(wDeviceID);
  133.             // format the window title line
  134.             wsprintf(szBuffer,"CD (%s) %d",
  135.                 (LPSTR)szStatus[iCDstatus],
  136.                 CD_GetCurrentTrack(wDeviceID));
  137.             // only update it if it changes as we don't like flashing
  138.             if(lstrcmp(szBuffer,szTitle)!=0) {
  139.                 lstrcpy(szTitle,szBuffer);
  140.                 SetWindowText(hDlg,szTitle);
  141.             }
  142.             break;
  143.  
  144.         case WM_SYSCOMMAND:
  145.         case WM_COMMAND:
  146.           switch(wParam) {
  147.               // show some useful info???
  148.             case IDM_STATUS:
  149.                 if((dwMCIerr=CD_ShowTrackTimes(wDeviceID,szBuffer))!=0L)
  150.                     showMCIError(dwMCIerr);
  151.                 break;
  152.  
  153.             // start play at track 1
  154.             case IDM_PLAY:
  155.                 if((dwMCIerr=CD_PlayTrack(wDeviceID,1,0))!=0L)
  156.                     showMCIError(dwMCIerr);
  157.                 else {
  158.                     iCDstatus=CD_PLAYING;
  159.                     wsprintf(szTitle,"CD (%s) %d",(LPSTR)szStatus[iCDstatus],
  160.                         CD_GetCurrentTrack(wDeviceID));
  161.                     SetWindowText(hDlg,szTitle);
  162.                 }
  163.                 break;
  164.  
  165.             // if the CD is playing, backup one track, else play
  166.             case IDM_BACK:
  167.                 if(iCDstatus==CD_PLAYING) {
  168.                     if((dwMCIerr=CD_ChangeTrack(wDeviceID,-1))!=0L)
  169.                         showMCIError(dwMCIerr);
  170.                 } else SendMessage(hDlg,WM_COMMAND,IDM_PLAY,0L);
  171.                 break;
  172.  
  173.             // if the CD is playing, forward one track, else play
  174.             case IDM_FORWARD:
  175.                 if(iCDstatus==CD_PLAYING) {
  176.                     if((dwMCIerr=CD_ChangeTrack(wDeviceID,1))!=0L)
  177.                         showMCIError(dwMCIerr);
  178.                 } else SendMessage(hDlg,WM_COMMAND,IDM_PLAY,0L);
  179.                 break;
  180.  
  181.             // if the CD is playing, pause it, else resume play
  182.             case IDM_PAUSE:
  183.                 if(iCDstatus==CD_PLAYING) {
  184.                     if((dwMCIerr=CD_Pause(wDeviceID))!=0L)
  185.                         showMCIError(dwMCIerr);
  186.                     else iCDstatus=CD_PAUSED;
  187.                 } else
  188.                     SendMessage(hDlg,WM_COMMAND,IDM_RESUME,0L);
  189.                 break;
  190.  
  191.             // if the CD is paused, resume play (play at current location)
  192.             case IDM_RESUME:
  193.                 if(iCDstatus==CD_PAUSED || iCDstatus==CD_STOPPED) {
  194.                     if((dwMCIerr=CD_ResumePlay(wDeviceID))!=0L)
  195.                         showMCIError(dwMCIerr);
  196.                     else {
  197.                         iCDstatus=CD_PLAYING;
  198.                         wsprintf(szTitle,"CD (%s) %d",(LPSTR)szStatus[iCDstatus],
  199.                             CD_GetCurrentTrack(wDeviceID));
  200.                         SetWindowText(hDlg,szTitle);
  201.                     }
  202.                 }
  203.                 break;
  204.  
  205.             // if the CD is playing or pause, stop it
  206.             case IDM_STOP:
  207.                 if(iCDstatus==CD_PLAYING || iCDstatus==CD_PAUSED) {
  208.                     if((dwMCIerr=CD_Stop(wDeviceID))!=0L)
  209.                         showMCIError(dwMCIerr);
  210.                     else iCDstatus=CD_STOPPED;
  211.                 }
  212.                 break;
  213.  
  214.             // untested (mine doesn't support) toggle door open/closed
  215.             case IDM_EJECT:
  216.                 if(iCDstatus!=CD_OPEN) {
  217.                     if((dwMCIerr=CD_Open(wDeviceID))!=0L)
  218.                         showMCIError(dwMCIerr);
  219.                     else iCDstatus=CD_OPEN;
  220.                 } else if((dwMCIerr=CD_Close(wDeviceID))!=0L)
  221.                         showMCIError(dwMCIerr);
  222.                     else iCDstatus=CD_STOPPED;
  223.                 break;
  224.  
  225.             // this is a test routine (activated by non-visible button)
  226.             case IDM_SHVOL:
  227.                 uAUXerr=auxGetVolume(wDeviceID,(LPDWORD)&volume);
  228.                 wsprintf(szBuf,"E:%u VL:%d VH:%d",uAUXerr,
  229.                     LOWORD(volume),HIWORD(volume));
  230.                 MessageBox(hDlg,szBuf,"CD Volume",MB_OK);
  231.                 break;
  232.  
  233.             // brag routine (actived by non-visible button)
  234.             case IDM_SHAUTH:
  235.                 MessageBox(hDlg,
  236.                 "Created by John A. Junod\nzj8549@trotter.usma.edu",
  237.                 "CD Author",MB_OK);
  238.                 break;
  239.  
  240.             // if they select close, turn off the timer and exit
  241.             case IDCANCEL:
  242.                 KillTimer(hDlg,1);
  243.                 EndDialog(hDlg,NULL);
  244.                 return(TRUE);
  245.             default:
  246.                 return(DefWindowProc(hDlg,message,wParam,lParam));
  247.           }
  248.           return(TRUE);
  249.  
  250.         case WM_CLOSE:
  251.             KillTimer(hDlg,1);
  252.             break;
  253.     }
  254.     return(FALSE);
  255. }
  256.  
  257. // use mciGetErrorString to display error
  258. void showMCIError(DWORD dwError) {
  259.     char szErrorBuf[MAXERRORLENGTH];
  260.  
  261.     MessageBeep(MB_ICONEXCLAMATION);
  262.     if(mciGetErrorString(dwError,(LPSTR)szErrorBuf,MAXERRORLENGTH))
  263.         MessageBox(hMainWnd,szErrorBuf,"MCI Error",MB_ICONEXCLAMATION);
  264.     else
  265.         MessageBox(hMainWnd,"Unknown Error","MCI Error",MB_ICONEXCLAMATION);
  266. }
  267.